package com.example.myui.activity;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.myui.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class TestRefrehActivity extends AppCompatActivity {

    private Unbinder bind;
    @BindView(R.id.smartrefreshlayout_test)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview_test)
    RecyclerView recyclerView;
    private TestAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_refreh);
        bind = ButterKnife.bind(this);

        View inflate = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new TestAdapter(null);
        recyclerView.setAdapter(adapter);
        adapter.setEmptyView(inflate);

        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refresh(refreshlayout, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refresh(refreshlayout, true);
            }
        });

    }

    private void refresh(RefreshLayout refreshlayout, boolean isRefresh) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isRefresh) {
                    adapter.setNewData(getDatas());
                    smartRefreshLayout.finishRefresh();
                } else {
                    adapter.addData(getDatas());
                    smartRefreshLayout.finishLoadmore();
                }
            }
        }, 2000);
    }

    private List<String> getDatas() {

        ArrayList<String> mDatas = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            mDatas.add("item :" + i);
        }
        return mDatas;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    private class TestAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        public TestAdapter(@Nullable List<String> data) {
            super(R.layout.item_text, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, String item) {
            holder.setText(R.id.tv_content, item);
        }
    }
}
